﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace VirtualDisk
{
    class Node
    {
        public enum NodeType
        {
            DNode,
            FNode
        }
        //节点的名字
        private string name;
         //节点的类型：目录还是文件
        private NodeType type;
        //节点的父亲
        private Node parrentNode=null;
        //节点的儿子列表
        private List<Node> childlist;
        //文件节点的数据
        private Byte[] data=null;
        //节点创建的时间
        private DateTime createDate;

        public string Name
        {
            get { return name;}
        }

        public DateTime CreateDate
        {
            get { return createDate; }
        }

        public bool IsFile
        {
            get { return type == NodeType.FNode; } 
        }

        public bool IsDirectory
        {
            get { return type == NodeType.DNode; }
        }

        public Node Parent 
        {
            get { return parrentNode; }
            set { parrentNode = value; }
        }

        //文件大小
        public int Size
        {
            get
            {
                if (data != null)
                {
                    return data.Length;
                }
                else
                    return 0;
            }
            

        }

        //文件数据
        public Byte[] FileData
        {
            get
            {
                if (data != null)
                {
                    return (Byte[])data.Clone();
                }
                else
                    return null;
            }
        }

        //添加儿子节点，并对儿子节点添加父亲
        public void addChild(Node child)
        {
            if (child==null)
            {
                return;
            }
            if (getChildren() == null)
            {
                return;
            }
            if (this.IsDirectory)
            {
                child.parrentNode = this;
                childlist.Add(child); 
            }
            
        }
        //去掉儿子节点，并去掉儿子节点的父亲
        public void RemoveChild(Node child)
        {
            if (child==null)
            {
                return;
            }
            
            if (getChildren()==null)
            {
                return;
            }
            if (this.IsDirectory)
            {
                child.parrentNode = null;
                childlist.Remove(child); 
            }
            
        }

        //儿子列表是否为空
        public bool isEmpty()
        {
            return (getChildren().Count == 0);
        }

        //流写入文件数据
        public void setData(Stream stm)
        {
            data = new Byte[stm.Length];
            using (BinaryReader rd = new BinaryReader(stm))
            {
                rd.Read(data, 0, data.Length);
            }
            
        }

        //获得节点的儿子列表
        public List<Node> getChildren()
        {
            return childlist;
        }

        //获得节点的完整路径
        public String getPath()
        {
            String path = this.name;
            if (this.parrentNode!=null)
            {
                path = this.parrentNode.getPath() + "\\" + this.name;
            }
            return path;
        }

        //获得名字为nodeName的子节点，不存在返回null
        public Node getChildByName(String nodeName)
        {
            if (nodeName == "..")
            {
                if(parrentNode!=null)
                    return parrentNode;
                else
                    return this;
            }
            foreach(Node child in childlist)
            {
                if (child.Name.ToLower()==nodeName.ToLower())
                {
                    return child;
                }
            }

            return null;
        }





        public Node(string name,NodeType type,DateTime time)
        {
            this.name = name;
            this.type = type;
            this.createDate = time;
            this.childlist = new List<Node>();
        }

        public override string ToString()
        {
            String type=(this.type==NodeType.DNode)?"<DIR>":"     ";
            String size = (this.type == NodeType.DNode) ? "  " : this.Size.ToString();
            return this.createDate.ToString() + "   " + type +"  "+ size+"    " + this.name;
        }

    }
}
